#encoding:utf-8

from exts import db
from datetime import datetime
from werkzeug.security import generate_password_hash,check_password_hash
from marshmallow import Schema,fields,post_load


class User(db.Model):
    __tablename__='user'
    id = db.Column(db.Integer,primary_key=True,autoincrement=True)
    telephone = db.Column(db.String(11),nullable=False)
    username = db.Column(db.String(20),nullable=False)
    password = db.Column(db.String(100),nullable=False)

    def __init__(self, *args, **kwargs):
        telephone = kwargs.get('telephone')
        username = kwargs.get('username')
        password = kwargs.get('password')

        self.telephone = telephone
        self.username = username
        self.password = generate_password_hash(password)

    def check_password(self,raw_password):
        result = check_password_hash(self.password,raw_password)
        return result
#消费记录
class Record(db.Model):
    __tablename__='record'
    id = db.Column(db.Integer,primary_key=True,autoincrement=True)
    title = db.Column(db.String(100),nullable=False)
    content = db.Column(db.Text,nullable=False)
    #now()为服务器第一次启动的时间，now每次创建模型时的时间
    create_time = db.Column(db.DateTime,default=datetime.now)
    author_id = db.Column(db.Integer,db.ForeignKey('user.id'))
    author = db.relationship('User',backref=db.backref('records',order_by=create_time.desc()))
    consumeType_id = db.Column(db.Integer,db.ForeignKey('consumeType.id'))
    consumeType = db.relationship('ConsumeType',backref=db.backref('records',order_by=create_time.desc()))


class Answer(db.Model):
    __tablename__='answer'
    id = db.Column(db.Integer,primary_key=True,autoincrement=True)
    content = db.Column(db.Text,nullable=False)
    create_time = db.Column(db.DateTime,default=datetime.now)
    author_id = db.Column(db.Integer,db.ForeignKey('user.id'))
    record_id = db.Column(db.Integer,db.ForeignKey('record.id'))

    author = db.relationship('User',backref=db.backref('answers'))
    #order_by 默认为升序
    record = db.relationship('Record',backref = db.backref('answers',order_by=create_time.desc()))


class ConsumeType(db.Model):
    __tablename__='consumeType'
    id = db.Column(db.Integer,primary_key=True,autoincrement=True)
    title = db.Column(db.String(20),nullable=False)

##-----------------------------

class RecordScheme(Schema):
    id = fields.Integer()
    title = fields.String()
    content = fields.String()
    create_time = fields.DateTime()
    author_id = fields.Integer()
    consumeType_id = fields.Integer()
    # author = fields.Nested(UserScheme, dump_to='author', many=True)

class UserScheme(Schema):
    id = fields.Integer()
    telephone = fields.String()
    username = fields.String()
    password = fields.String()
    records = fields.Nested(RecordScheme, dump_to='records', many=True)

    @post_load
    def make_user(self, data):
        return User(**data)


class ConsumeTypeScheme(Schema):
    id = fields.Integer()
    title = fields.String()



        # title_list = ['餐饮', '购物', '日用', '交通', '蔬菜', '水果', '零食', '运动', '娱乐', '通讯', '服饰', '美容', '住房', '居家', '孩子', '长辈',
        #               '社交', '旅行', '烟酒', '数码', '汽车', '医疗', '书籍', '学习', '宠物', '礼金', '礼物', '办公', '维修', '捐赠', '彩票', '设置']
        # object_list = []
        # dict_list = []
        # for title2 in title_list:
        #     titleModel = ConsumeType(title=title2)
        #     object_list.append(titleModel)
        #
        # print object_list
        #
        # for item in object_list:
        #     item_dict = item.__dict__
        #     if "_sa_instance_state" in item_dict:
        #         del item_dict["_sa_instance_state"]
        #         dict_list.append(item_dict)
        #
        # print dict_list
        #
        # # db.session.execute(
        # #     [db.session.add(consumeTypeModel)  for u in object_list]
        # # )
        # db.session.execute(ConsumeType.__table__.insert(dict_list))
        # db.session.commit()
